#!/usr/bin/python2

import pygame

from utils import Constants


class Key(pygame.sprite.Sprite):

    def __init__(self, x, y, number, pattern, groups=tuple()):
        # Initialize parent
        pygame.sprite.Sprite.__init__(self, groups)
        # Set the image and bounds
        self.image = pygame.surface.Surface((Constants.GRID_SIZE, ) * 2,
                                            pygame.SRCALPHA)
        color = pygame.color.Color(Constants.KEY_COLOR)
        for r in xrange(min(len(pattern), Constants.KEY_SIZE)):
            for c in xrange(min(len(pattern[r]), Constants.KEY_SIZE)):
                if pattern[r][c].strip():
                    pygame.draw.rect(self.image, color, (c, r, 2, 2))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.__num = number

    def unlock(self, groups):
        if "_door%s" % self.__num in groups:
            map(lambda s: s.kill(), groups["_door%s" % self.__num])
        self.kill()

    def update(self, delta, rotation, groups):
        pass
